/**
 * 
 */
package nl.boektrust.btsoep.client;

import nl.boektrust.btsoep.shared.model.ContentSpecifier;
import nl.boektrust.btsoep.shared.model.ISBN;
import nl.boektrust.btsoep.shared.model.ContentSpecifier;
import nl.boektrust.btsoep.shared.model.ContentSpecifierType;

import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;

/**
 * @author rao3
 *
 */
public  class EditBookSubjectPanel extends EditBookPanel implements ClickHandler, ValueChangeHandler<String>, KeyPressHandler, ChangeHandler {
	private ListBox subjectTypeListBox = new ListBox();
	private TextBox subjectTextBoxDescription;
	private TextBox subjectTextBoxCode;
	private Button xButton = new Button("ok");;
	private ContentSpecifier contentSpecifier;
	private boolean isNewField = false;
	int numFocusedElements = 1;
	private int index;

	public EditBookSubjectPanel(String label, ContentSpecifier contentSpecifier, boolean isInEditMode) {
		super(label, isInEditMode);
		this.contentSpecifier = contentSpecifier;

		this.subjectTypeListBox = new ListBox();
		for (ContentSpecifierType t : ContentSpecifierType.values()) {
			this.subjectTypeListBox.addItem(ContentSpecifierType.stringValue(t));			
		}
		this.subjectTextBoxCode = new TextBox();
		this.subjectTextBoxCode.addValueChangeHandler(this);
		this.subjectTextBoxCode.setWidth("5em");

		this.subjectTextBoxDescription = new TextBox();
		this.subjectTextBoxDescription.addValueChangeHandler(this);
		this.subjectTextBoxDescription.setWidth("15em");
		this.subjectTypeListBox.addChangeHandler(this);
		if (contentSpecifier != null) {
			this.contentSpecifier = contentSpecifier;
			isNewField = false;
		} else {
			this.contentSpecifier = new ContentSpecifier();
			isNewField = true;
		}
		xButton.addClickHandler(this);

		editPanel.add(subjectTypeListBox);
		editPanel.add(subjectTextBoxDescription);
		editPanel.add(subjectTextBoxCode);
		editPanel.add(xButton);

		
		if (isInEditMode == true) {
			setToEditMode();
		} else {
			setToDisplayMode();
		}
	}
	
	/**
	 * @return the contentSpecifier
	 */
	public ContentSpecifier getIsbnReference() {
		return contentSpecifier;
	}

	/**
	 * @param contentSpecifier the contentSpecifier to set
	 */
	public void setIsbnReference(ContentSpecifier contentSpecifier) {
		this.contentSpecifier = contentSpecifier;
	}


	public EditBookSubjectPanel(String label, boolean isInEditMode) {
		this(label, null, isInEditMode);
	}

	public EditBookSubjectPanel(boolean isInEditMode) {
		this("", isInEditMode);
	}

	public EditBookSubjectPanel(String label) {
		this(label, false);
	}

	public EditBookSubjectPanel() {
		this(false);
	}
	
	boolean hasInvalidData() {
		return false;
	}



	@Override
	void setToEditMode() {
		super.setToEditMode();
		if (isNewField == false) {
			subjectTextBoxDescription.setText(contentSpecifier.getDescription());
			subjectTextBoxCode.setText(new Integer(contentSpecifier.getCode()).toString());
			// loop over all the elements in the listbox until the one is found that matches the text of the current ContentSpecifierType
			for (int i=0; i<subjectTypeListBox.getItemCount(); i++) {
				if (subjectTypeListBox.getItemText(i).equals(ContentSpecifierType.stringValue(contentSpecifier.getContentSpecifierType()))) {
					subjectTypeListBox.setSelectedIndex(i);
				}
			}
		}
	
		if (!(subjectTypeListBox.getItemText(subjectTypeListBox.getSelectedIndex()).equals("onbekend"))) {
			if (ContentSpecifierType.isNumericValue(contentSpecifier.getContentSpecifierType()) == true) {
				subjectTextBoxCode.setVisible(true);
				subjectTextBoxDescription.setVisible(false);
			} else {
				subjectTextBoxCode.setVisible(false);
				subjectTextBoxDescription.setVisible(true);
			}
		} else {
			subjectTextBoxCode.setVisible(false);
			subjectTextBoxDescription.setVisible(false);
		}
		
	}

	/**
	 * @return the contentSpecifier
	 */
	public ContentSpecifier getContentSpecifier() {
		return contentSpecifier;
	}

	/**
	 * @param contentSpecifier the contentSpecifier to set
	 */
	public void setContentSpecifier(ContentSpecifier contentSpecifier) {
		this.contentSpecifier = contentSpecifier;
		isNewField = false;
	}

	@Override
	void setToDisplayMode() {
		super.setToDisplayMode();
		if (isNewField == false) {
			try {
				if (contentSpecifier != null && contentSpecifier.getContentSpecifierType() != ContentSpecifierType.UNDEFINED) {
					setFieldLabel(ContentSpecifierType.stringValue(contentSpecifier.getContentSpecifierType()));
					switch (contentSpecifier.getContentSpecifierType()) {
					case NUGICODE :
					case NURCODE :
						theLabel.setText(""+	contentSpecifier.getCode());
						break;

					case GEOCODE :
						theLabel.setText(contentSpecifier.getDescription());
						break;

					default :
						System.err.println("EditBookSubjectPanel:setToDisplayMode: error, contentSpecifier.getContentSpecifierType returned an unknown type, cannot display");
					}	
				} else {
					theLabel.setText("Voeg nieuw onderwerp toe...");
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			theLabel.setText("Voeg nieuw onderwerp toe");
		}
	}
	
	public void onClick(ClickEvent event) {
		if (event.getSource() == xButton) {
			setInEditMode(false);
			this.setToDisplayMode();
			valueHasChanged(contentSpecifier, index);
		}
		
	}

	

	public void onValueChange(ValueChangeEvent event) {
		if (event.getSource()== subjectTextBoxDescription) {
			try {
				contentSpecifier.setDescription(subjectTextBoxDescription.getText());
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else if (event.getSource()== subjectTextBoxCode) {
			try {
				contentSpecifier.setCode(new Integer(subjectTextBoxCode.getText()).intValue());
			} catch (Exception e) {
				e.printStackTrace();
			}
		} 
	}

	public void onKeyPress(KeyPressEvent event) {
	/*	if (event.getSource() == kleurTextBox || event.getSource() == zwTextBox) {
			if (Character.isLetter(event.getCharCode()) || Character.isSpace(event.getCharCode())) {
		          ((TextBox) event.getSource()).cancelKey();
		        }
		}*/
		
	}
	
	public void valueHasChanged(ContentSpecifier c, int index) {}

	public void onChange(ChangeEvent event) {
		if (event.getSource() == subjectTypeListBox) {
			for (ContentSpecifierType t : ContentSpecifierType.values()) {
				if (ContentSpecifierType.stringValue(t).equals(subjectTypeListBox.getItemText(subjectTypeListBox.getSelectedIndex()))) {
					contentSpecifier.setContentSpecifierType(t);
				}
			}
			if (ContentSpecifierType.isNumericValue(contentSpecifier.getContentSpecifierType()) == true) {
				subjectTextBoxDescription.setText("");
			} else {
				subjectTextBoxCode.setText("");
			}
			this.setToEditMode();

		}
	}

	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}

	


}
